package com.yzy.fly.loader;

import com.yzy.fly.entity.role.FlyView;
import com.yzy.fly.entity.role.FlyViewWidget;
import com.yzy.fly.exception.FunctionNotImplementException;
import com.yzy.fly.kernel.model.ability.tree.RecursionTreeLoader;
import com.yzy.fly.kernel.model.ability.tree.TreeLoader;
import com.yzy.fly.mapper.FlyViewMapper;
import com.yzy.fly.mapper.FlyViewMatchMapper;
import com.yzy.fly.mapper.FlyViewWidgetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 模型加载器
 */
@Component
public class ViewLoader {
    @Autowired
    private FlyViewMapper flyViewMapper;
    @Autowired
    private FlyViewWidgetMapper flyViewWidgetMapper;
    @Autowired
    private FlyViewMatchMapper flyViewMatchMapper;

    public FlyView loader(int viewId) {
        FlyView flyView = flyViewMapper.selectById(viewId);
        if (flyView != null) {
            List<FlyViewWidget> flyViewWidgets = flyViewWidgetMapper.selectAllByViewId(viewId);
            List<FlyViewWidget> loader = null;
            try {
                loader = (List<FlyViewWidget>) new RecursionTreeLoader().loader(flyViewWidgets);
            } catch (FunctionNotImplementException e) {
                e.printStackTrace();
            }
            flyView.setWidgetList(loader);
        }

        return flyView;
    }
}
